﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APPLICATION.Dict;
using System.Data.SqlClient;
namespace APPLICATION.Dict.Clients
{
    public partial class AddClients : Form
    {
        private string ClientsID ;
        public AddClients(string PClientsID)
        {
            InitializeComponent();
            ClientsID = PClientsID;
            choiserCity.ConnectionString = globals.APPCONNSTRING;
        }

        public AddClients()
        {
            InitializeComponent();
        }

        private bool Checking()
        {
            if (textBox1.isEmpty)
            {
                System.Windows.Forms.MessageBox.Show(this, "The required Client Name field is empty", "Client", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                textBox1.Focus();
                return false;
            }
            else if(string.IsNullOrEmpty(choiserCity.TextBoxSearch.Text))
            {
                System.Windows.Forms.MessageBox.Show(this, "The required City field is empty", "Client", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                choiserCity.TextBoxSearch.Focus();
                return false;
            }
            else if (txtIdentificationCode.isEmpty)
            {
                System.Windows.Forms.MessageBox.Show(this, "The required Client Identification Code field is empty", "Client", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                txtIdentificationCode.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        private void AddClients_Load(object sender, EventArgs e)
        {
            choiserCity.ConnectionString = globals.APPCONNSTRING;
            //if PClientsID is null then we need to add new record
            if (string.IsNullOrEmpty(ClientsID))
            {
                calendarTextBox1.Text = DateTime.Today.Date.ToShortDateString();
            }
            else
            {
                
                DataView dw = new DataView(dataSet1.Clients);
                bindingSource1.DataSource = dw;
                bindingSource1.Filter = "ClientsID=" + ClientsID;
                

                #region binding to controls
                textBox1.DataBindings.Add("Text", bindingSource1, "ClName");
                choiserCity.TextBoxSearch.DataBindings.Add("Tag", bindingSource1, "CityID");

                txtAdress.DataBindings.Add("Text", bindingSource1, "address");
                txtPostalCode.DataBindings.Add("Text", bindingSource1, "PostalCode");
                txtPhoneNumber.DataBindings.Add("Text", bindingSource1, "Phone");
                txtFax.DataBindings.Add("Text", bindingSource1, "Fax");
                txtMobilePhone.DataBindings.Add("Text", bindingSource1, "Mobile");
                txtEmail.DataBindings.Add("Text", bindingSource1, "Emaill");
                txtContactPerson.DataBindings.Add("Text", bindingSource1, "Person");
                txtManager.DataBindings.Add("Text", bindingSource1, "Meneger");
                txtLicenseNumber.DataBindings.Add("Text", bindingSource1, "Lit");
                calendarTextBox1.DataBindings.Add("Text", bindingSource1, "Dlit");
                txtIdentificationCode.DataBindings.Add("Text", bindingSource1, "Sn");
                memoEdit1.DataBindings.Add("Text", bindingSource1, "addInf");

                #endregion

            }
        }

        
        private void btmAddEditClient_Click(object sender, EventArgs e)
        {

            SqlCommand SqlCmd = new SqlCommand();
            //if PClientsID is null then we need to add new record
            if (string.IsNullOrEmpty(ClientsID))
            {                
                SqlCmd.Parameters.Add("Action", SqlDbType.TinyInt).Value = 0;
                SqlParameter SqlParam = new SqlParameter("ClientsID", SqlDbType.Int);                
                SqlParam.Direction = ParameterDirection.Output;
                SqlCmd.Parameters.Add(SqlParam);
            }
            else
            {
                SqlParameter SqlParam = new SqlParameter("ClientsID", SqlDbType.Int);
                SqlParam.Direction = ParameterDirection.InputOutput;
                SqlCmd.Parameters.Add(SqlParam).Value=ClientsID;
                SqlCmd.Parameters.Add("Action", SqlDbType.TinyInt).Value = 1;
            }

            if (Checking())
            {
                SqlCmd.Parameters.Add("ClName", SqlDbType.NVarChar, 100).Value = textBox1.Text;
                //SqlCmd.Parameters.Add("CityID", SqlDbType.Int).Value = choiserCity.ResultManager.GetResult(choiserCity.r)
                SqlCmd.Parameters.Add("address", SqlDbType.NVarChar, 100).Value = txtAdress.Text;
                SqlCmd.Parameters.Add("PostalCode", SqlDbType.VarChar, 10).Value = txtPostalCode.Text;
                SqlCmd.Parameters.Add("Phone", SqlDbType.NVarChar, 100).Value = txtPhoneNumber.Text;
                SqlCmd.Parameters.Add("Fax", SqlDbType.NVarChar, 100).Value = txtFax.Text;
                SqlCmd.Parameters.Add("Mobile", SqlDbType.NVarChar, 100).Value = txtMobilePhone.Text;
                SqlCmd.Parameters.Add("Emaill", SqlDbType.NVarChar, 100).Value = txtEmail.Text;
                SqlCmd.Parameters.Add("Person", SqlDbType.NVarChar, 100).Value = txtContactPerson.Text;
                SqlCmd.Parameters.Add("Meneger", SqlDbType.NVarChar, 100).Value = txtManager.Text;
                SqlCmd.Parameters.Add("Lit", SqlDbType.Char, 10).Value = txtLicenseNumber.Text;
                SqlCmd.Parameters.Add("Dlit", SqlDbType.DateTime).Value = calendarTextBox1.Text;
                SqlCmd.Parameters.Add("Sn", SqlDbType.NVarChar, 20).Value = txtIdentificationCode.Text;
                dataSet1.InsertDataClient(SqlCmd);
            }
        }

        private void textBox1_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
